import os
import shutil


def change_reid_name(dir_path, stored_dir):
    """
    :param dir_path:剪切好的人像文件夹，目录格式为：
           ----- dir_path
                ----- 用户序号 1
                     ----- 图片 1
                     ----- 图片 2
                     ----- 图片 3
                ----- 用户序号 2
                     ----- 图片 1
                     ----- 图片 2
                     ----- 图片 3
                ----- 用户序号 3
                     ----- 图片 1
                     ----- 图片 2
                     ----- 图片 3
    :param stored_dir:用户画像存储文件夹
    :return:
    """
    dir_list = os.listdir(dir_path)
    dir_list.sort()
    print(dir_list)
    for dir_name in dir_list:
        picture_dir = os.path.join(dir_path, dir_name)
        # 获取文件夹内的所有图片
        picture_list = os.listdir(picture_dir)
        picture_list.sort()
        picture_num = 0
        for picture in picture_list:
            picture_num += 1
            # 确定乘客序号
            full_name = dir_name + "_"
            # 确定摄像头序号
            if "C167" in picture:
                full_name += "c1s1_"
            elif "C172" in picture:
                full_name += "c2s1_"
            else:
                full_name += "c3s1_"
            # 确定用户画像序号
            order_num = str(picture_num)
            zero_num = 4 - len(order_num)
            order_num = "0" * zero_num + order_num
            # 补全名称
            full_name += (order_num + "_01.jpg")
            old_name = os.path.join(picture_dir, picture)
            new_name = os.path.join(picture_dir, full_name)
            stored_name = os.path.join(stored_dir, full_name)
            os.rename(old_name, new_name)
            print(old_name, '======>', new_name)
            shutil.move(new_name, stored_name)


if __name__ == "__main__":
    dir_path = r"D:\Develop\DataSet\Camera\gallery-done"
    stored_dir = r"D:\Develop\DataSet\Camera\all"
    change_reid_name(dir_path, stored_dir)
